upload = {
    up : function(){
        $("body").on("change",".upfile", function(){
            var actionType = $(this).attr("data-action-type");
	    var dataID = $(this).attr("data-action-id");
	    var img = $(this).val();
	    $("#uploadFilename_" + dataID).html(img);
/* ========== User Personal Photo and Company logo ========== */          
            if (actionType=="userPhoto") {
                $(".loaderUploadProfile").show();
                $("#personal_photo").ajaxForm(function(data){
                    //console.log(data)
                    if (data=="Invalid File Format") {
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".filename").html("No file selected");
                        $(".loaderUploadProfile").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else if(data=="File too large. File must be less than 4 megabytes."){
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".filename").html("No file selected");
                        $(".loaderUploadProfile").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else if(data=="It seems that you have uploaded a nude picture."){
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".filename").html("No file selected");
                        $(".loaderUploadProfile").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else{
                        var json = $.parseJSON(data);
                        
                        if(json[0].successful=="Your new photo was successfully uploaded."){
                                showNotification({
                                        message: json[0].successful,
                                        type: "success",
                                        autoClose: true,
                                        duration: 3
                                });
                                $(".personalPhoto, .userProfile").fadeOut();
                                $(".personalPhoto, .userProfile").fadeIn();
                                $(".personalPhoto, .userProfile").attr("src",json[0].image);
                                $(".filename").html("No file selected");
                                $(".loaderUploadProfile").hide();
				$("#uploadFilename_" + dataID).html("No file selected");
                        }   
                    }
                    
                }).submit();    
            }else if (actionType=="companyPhoto") {
                $(".loaderUploadCompany").show();
                $("#company_photo").ajaxForm(function(data){
                    //console.log(data)
                    if (data=="Invalid File Format") {
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".filename").html("No file selected");
                        $(".loaderUploadCompany").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else if(data=="File too large. File must be less than 4 megabytes."){
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".filename").html("No file selected");
                        $(".loaderUploadCompany").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else if(data=="It seems that you have uploaded a nude picture."){
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".filename").html("No file selected");
                        $(".loaderUploadCompany").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else{
                        var json = $.parseJSON(data);
                        if(json[0].successful=="Your new photo was successfully uploaded."){
						console.log(json)
                                showNotification({
                                        message: json[0].successful,
                                        type: "success",
                                        autoClose: true,
                                        duration: 3
                                });
                                $(".companyPhoto, .companyPic").fadeOut();
                                $(".companyPhoto, .companyPic").fadeIn();
                                $(".companyPhoto, .companyPic").attr("src",json[0].image);
                                $(".filename").html("No file selected");
                                $(".loaderUploadCompany").hide();
				$("#uploadFilename_" + dataID).html("No file selected");
                        }
                    }
                    
                }).submit(); 
            }
/* ========== Upload Form Logo ========== */

            else if (actionType=="uploadLogoForm") {
                $(".postLogoLoader").show();
              
                $("#formLogo").ajaxForm(function(data){
                    if (data=="Invalid File Format") {
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".postLogoLoader").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else if(data=="File too large. File must be less than 4 megabytes."){
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".postLogoLoader").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else if(data=="It seems that you have uploaded a nude picture."){
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".postLogoLoader").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else{
                        var json = $.parseJSON(data);
			$("body").data("form_logo",json);
			//console.log(json)
                        showNotification({
                                message: "Your photos was successfully uploaded.",
                                type: "success",
                                autoClose: true,
                                duration: 3
                        });
			$(".form_logo").attr("src",json[0].image); // Set Image to a form when done uploading
                        $(".postLogoLoader").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }
                }).submit(); 
            }
/* ========== Upload Form Logo ========== */

            else if (actionType=="upload_form_icon_image") {
                $(".postFileLoad").show();
              
                $("#postFile").ajaxForm(function(data){
                    if (data=="Invalid File Format") {
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".postFileLoad").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else if(data=="File too large. File must be less than 4 megabytes."){
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".postFileLoad").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else if(data=="It seems that you have uploaded a nude picture."){
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });$(".postFileLoad").hide();$(".postLogoLoader").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else{
                        var json = $.parseJSON(data);
			$("body").data("form_logo_img",json);
			//console.log(json)
                        showNotification({
                                message: "Your photos was successfully uploaded.",
                                type: "success",
                                autoClose: true,
                                duration: 3
                        });
			$(".imagePrev").attr("src",json[0].image); // Set Image to a form when done uploading
                        $(".postFileLoad").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }
                }).submit(); 
            }            
/* ========== Attach File on request ========== */
            else if (actionType=="attachFile") {
                $(".attachFiles").show();
                var ret = "";
                $("#fille_attach").ajaxForm(function(data){
                    if (data=="Invalid File Format") {
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".attachFiles").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else if(data=="File too large. File must be less than 4 megabytes."){
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".attachFiles").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else if(data=="It seems that you have uploaded a nude picture."){
                        showNotification({
                                message: data,
                                type: "error",
                                autoClose: true,
                                duration: 3
                        });
                        $(".attachFiles").hide();
			$("#uploadFilename_" + dataID).html("No file selected");
                    }else{
                        var json = $.parseJSON(data);
                        showNotification({
                                message: "Attached file was successfully added.",
                                type: "success",
                                autoClose: true,
                                duration: 3
                        });
                        $(".attachFiles").hide();
                        
                        if (json[0].extension=="file") {
                            ret += '<div>';
                            ret += '<form class="pull-left" method="POST" >';
                            ret += '<div>';
                            ret += '<input type="submit" class="cursor tip" data-original-title="Download Attachment"/><i class="fa fa-download icon-download"></i> '; 
                            ret += '<i class="fa fa-trash-o icon-trash cursor tip removeFiles" data-original-title="Remove Attachment" data-file="' + json[0].filename + '" data-location="/' + json[0].img + '"></i>';
                            ret += '</div>';
                            ret += '<a class="download_attachment">';
                            ret += '<img data-name="' + json[0].filename + '" src="/'+ json[0].img + '" width="25" height="25" class="AFiles imagetoPost avatar userPhoto personalPhoto userAvatar" data-original-title="'+ json[0].filename + '" title="' + json[0].filename + '">';
                            ret += '</a>';
                            //ret += '<input type="submit"/>';
                            ret += '<input type="hidden" value="'+ json[0].filename +'" name="attachment_filename"/>';
                            ret += '<input type="hidden" value="' + json[0].img + '" name="attachment_location"/>';
                            
                            ret += '</form>';
                            ret += '</div>';
                            $(".attached_file_container").append(ret);
                        }else{
                            ret += '<div>';
                            ret += '<form class="pull-left" method="POST" >';
                            ret += '<div>';
                            ret += '<input type="submit" class="cursor tip" data-original-title="Download Attachment"/><i class="fa fa-download icon-download"></i> '; 
                            ret += '<i class="fa fa-trash-o icon-trash cursor tip removeFiles" data-original-title="Remove Attachment" data-file="' + json[0].filename + '" data-location="/' + json[0].img + '"></i>';
                            ret += '</div>';
                            ret += '<a class="download_attachment">';
                            ret += '<div data-name="'+ json[0].filename +'" class="AFiles_only avatar AFiles tip" data-original-title="'+ json[0].filename +'" title="'+ json[0].filename +'"><i class="fa fa-file icon-file"></i></div>';
                            ret += '</a>';
                            //ret += '<input type="submit"/>';
                            ret += '<input type="hidden" value="'+ json[0].filename +'" name="attachment_filename"/>';
                            ret += '<input type="hidden" value="' + json[0].img + '" name="attachment_location"/>';
                            
                            ret += '</form>';
                            ret += '</div>';
                            $(".attached_file_container").append(ret);
                        }
                        
                        // ADD to Display FrmRequest
                        var files_attachment = $(".AFiles").map(function(index){
                            return $(this).attr("data-name");
                        }).get().join();
                        $("#view_attachment_files").val(files_attachment);
                        //console.log($(".attached_file_container > div").length);
                        $(".tip").tooltip();
                        if ($(".attached_file_container > div").length=="1") {
                           $("<br><br>").insertAfter(".attached_file_container");
                        }
			$("#uploadFilename_" + dataID).html("No file selected");
                    }
                }).submit(); 
            }
        });
    }
}
$(document).ready(function(){
    upload.up(); // Uploading profile pic
    
    
    $('body').on('click','.form_upload_photos[data-upload="photo_upload"]',function(){
	var dataOBJID = $(this).attr("data-object-id");
	//var c = $(this).children().children().children().html();
	var getFormID = $("#getFormID").val();
	var getID = $("#getID").val();
	var getTrackNo = $("#getTrackNo").val();
	var ret = '<h3><i class="icon-save"></i> Upload Image</h3>'+
	'<div class="hr"></div>'+
	'<form id="getpostSelectFormPhotos_' + dataOBJID + '" method="post" enctype="multipart/form-data" action="/ajax/formUpload" style="z-index: 1000000000000;position: relative;">'+
	    //'<input type="file" data-action-id="2" value="upload" name="image" id="formImage" size="24" data-action-type="postImageForm" style="opacity: 0;position: absolute;" class="">'+
	    //'<i class="icon-picture"></i> Image'+
	    '<div id="uniform-fileInput" class="uploader2">'+
	    '<input type="file" data-action-id="' + dataOBJID + '" value="upload" name="image" id="postSelectFormPhotos_' + dataOBJID + '" size="24" data-action-type="postImageForm">'+
		    '<span id="uploadFilename_2" class="filename">No file selected</span>'+
		    '<span class="action">Choose File</span>'+
		    '</div>'+
	    '</div>'+
	'</form>'+
	'<div class="hr"></div>'+
	'<div class="fields"><div class="label_basic"></div><div class="input_position" style="margin-top:5px;text-align:right;"><img src="/images/loader/load.gif" class="display saveFormLoad" style="margin-right:5px;margin-top:2px;"><input type="button" class="btn-basicBtn" id="popup_cancel" value="Cancel"></div></div>';
	
	jDialog(ret, "", "300", "", "", function() {
	});
	// Upload Image to the form 
        $("#postSelectFormPhotos_" + dataOBJID).change(function() {
            var dataID = $(this).attr("data-action-id");
            $("#getpostSelectFormPhotos_" + dataOBJID).ajaxForm(function(data) {
		console.log(data)
                if (data == "Invalid File Format") {
                    showNotification({
                        message: data,
                        type: "error",
                        autoClose: true,
                        duration: 3
                    });
                    $(".filename").html("No file selected");
                } else if (data == "File too large. File must be less than 4 megabytes.") {
                    showNotification({
                        message: data,
                        type: "error",
                        autoClose: true,
                        duration: 3
                    });
                    $(".filename").html("No file selected");
                } else {
                    
                    var json = $.parseJSON(data);
                    console.log(json)
                    if (json[0].successful == "Your new photos was successfully uploaded.") {
                        //imageForm = $(getimageForm(count, "imageForm", json[0].img));
                        $(".ImagegetFields_" + dataID).attr("src",json[0].img);
			$(".getFields_" + dataID).attr("value",json[0].img);
			console.log(json[0].img)
			$("#popup_cancel").trigger('click');
		       
                    }
                }
            }).submit();
        });
    });
})